//
//  MainSearchViewController.m
//  棒棒哒
//
//  Created by rimi on 15/8/10.
//  Copyright (c) 2015年 rimi. All rights reserved.
//

#import "MainSearchViewController.h"
#import "Config.h"
#import "DetailDiseaseInfoViewController.h"


@interface MainSearchViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic ,strong)UIPickerView *pickerView;
@property (nonatomic ,strong)UITextField  *diseaseInfo;
@property (nonatomic ,strong)UIAlertController *alert;
@property (nonatomic ,strong)NSMutableArray *departmentNames;
@property (nonatomic ,strong)NSMutableArray *organs;
@property (nonatomic ,assign)NSInteger currentDepartment;
@property (nonatomic ,assign)NSInteger currentOrigin;

@end

@implementation MainSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeInterface];
    [self networkRequest];
}

-(void)initializeDataSource
{
//    _departmentNames = [NSMutableArray array];
//    _organs = [NSMutableArray array];
    
}

-(void)initializeInterface
{
    self.view.backgroundColor = [UIColor whiteColor];

    
    

    
//    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(16, 64 +5, SCREEN_WIDTH - 32, SCREEN_HEIGHT/2)];
//    backView.layer.borderWidth = 1;
//    backView.layer.cornerRadius = 5;
//    backView.layer.borderColor = [[UIColor grayColor]CGColor];
//    [self.view addSubview:backView];
    
    

//    _pickerView.center = backView.center;
//    NSLog(@"pickerViewCenter = %@\nbackViewCenter = %@",NSStringFromCGPoint(_pickerView.center),NSStringFromCGPoint(backView.center));
////    [_pickerView sizeToFit];
//    [backView addSubview:_pickerView];
    
    
//    _alert = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n\n" message:@"" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        
//        //传参并推送查找
//    }];
//    UIAlertAction *quit = [UIAlertAction actionWithTitle:@"取消" style:1 handler:^(UIAlertAction *action) {
//        [_alert dismissViewControllerAnimated:YES completion:nil];
//    }];
//    [_alert addAction:sure];
//    [_alert addAction:quit];
    
    
    
    

    
}







-(void)loadAfterNetworkRequest
{
    self.title = @"疾病信息";
    _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(16, 64 +5, SCREEN_WIDTH - 32, SCREEN_HEIGHT/2)];
    _pickerView.dataSource = self;
    _pickerView.delegate = self;
    _pickerView.layer.cornerRadius = 5;
    _pickerView.layer.borderWidth = 1;
    _pickerView.layer.borderColor = [[UIColor grayColor]CGColor];
    
    [self.view addSubview:_pickerView];
    //设置确定按钮
    UIButton *pickButton = [CustomButton buttonWithFrame:CGRectZero Title:@"查看疾病种类" BackGroudImage:nil BackGroundColor:[UIColor yellowColor] TextColor:[UIColor blackColor] CornerRadius:5 BorderWidth:1 BorderColor:[UIColor grayColor]];
    pickButton.titleLabel.font = [UIFont systemFontOfSize:15];
    pickButton.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT /2 +10);
    pickButton.bounds = CGRectMake(0 ,0, 100, 35);
    [pickButton addTarget:self action:@selector(handleButtonPick) forControlEvents:64];
    [self.view addSubview:pickButton];
    
    
    
    
    

    
}
-(void)handleButtonPick
{
    DetailDiseaseInfoViewController *detailDisease = [[DetailDiseaseInfoViewController alloc]init];
    detailDisease.departmentID = [NSString stringWithFormat:@"%@",_departmentNames[_currentDepartment][@"id"]];
    detailDisease.originID = [NSString stringWithFormat:@"%@",_organs[_currentOrigin][@"id"]];
    [self.navigationController pushViewController:detailDisease animated:YES];
    
//    NSLog(@"%@,%@",_cu)
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 4;
    
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return 1;
    }else if (component == 1){
    
        return _departmentNames.count;
    
    }else if (component == 2){
        
        return 1;
    }else{
        return _organs.count;
    }
    
   
    
    
    
    
}
#pragma mark - UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 1) {
        _currentDepartment = row;
    }else if (component == 3){
        _currentOrigin = row;
    }

}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *titleLabel = (UILabel *)view;
    if (!titleLabel) {
        titleLabel = [[UILabel alloc ]init];
       
        titleLabel.font = [UIFont systemFontOfSize:14];
    }
    //配置文字
    if (component == 0) {
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.text = @"  就诊科室:";
        return titleLabel;
    }else if (component == 1){
        titleLabel.text = _departmentNames[row][@"name"];
        return titleLabel;
    }else if (component == 2){
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.text = @"  身体部位:";
        return titleLabel;
    }else{
        titleLabel.text =_organs[row][@"name"];
        return titleLabel;
    }

}



-(void)networkRequest
{
    self.title = @"加载中..";
    NSDictionary *dic = @{@"key":APIKEY};
    [NetRequest GET:DISEASE parameters:dic success:^(id responseObject) {
        _departmentNames = [responseObject[@"yi18"]mutableCopy];
        [NetRequest GET:@"http://api.1ccf.com/disease/place" parameters:dic success:^(id responseObject) {
            _organs = [responseObject[@"yi18"]mutableCopy];
            [self loadAfterNetworkRequest];
        } failure:^(NSError *error) {
            
        }];
    } failure:^(NSError *error) {
        
        
    }];
    
}
@end
